﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Congresso_Salao_relatorios_LiberarCertificado : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PreencherGrid();    
        }
    }
    
    private void PreencherGrid()
    {
        SqlCommand comSelect = new SqlCommand("SELECT CodInscrito, Nome, CertificadoLiberado FROM FaculdadeEncontroInscritos WHERE CodEvento = @CodEvento AND efetivado = 1 ORDER BY Nome;");
        comSelect.Parameters.Add(new SqlParameter("CodEvento", Session["codEvento"].ToString()));
        DataTable dt = executarQuery(comSelect);

        if (dt != null && dt.Rows.Count > 0)
        {
            this.divAvisoSemGridView.Visible = false;
            gridViewInscritos.DataSource = dt;
            gridViewInscritos.DataBind();
        }
        else
        {
            this.divAvisoSemGridView.Visible = true;
        }
    }

    protected void grdItens_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridViewInscritos.PageIndex = e.NewPageIndex;
        PreencherGrid();
    }
        
    protected void chkview_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        GridViewRow gvr = (GridViewRow)cb.NamingContainer;
        string id = gridViewInscritos.Rows[gvr.RowIndex].Cells[0].Text.ToString();

        int certificadoLiberado = 0;
        if (cb.Checked)
            certificadoLiberado = 1;
        else
            certificadoLiberado = 0;

        try
        {
            SqlCommand comUpdate = new SqlCommand("UPDATE FaculdadeEncontroInscritos SET CertificadoLiberado = @CertificadoLiberado, AlteracaoCertificadoPor = @AlteracaoCertificadoPor WHERE CodInscrito = @codInscrito");
            comUpdate.Parameters.Add(new SqlParameter("CertificadoLiberado", certificadoLiberado));
            comUpdate.Parameters.Add(new SqlParameter("AlteracaoCertificadoPor", Session["NomeUsuario"].ToString()));
            comUpdate.Parameters.Add(new SqlParameter("CodInscrito", id));
            executarQuery(comUpdate);

            PreencherGrid();
        }
        catch (Exception exc)
        {
            MetodoException(exc.Message, "CheckedChanged");
        }
    }

    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        try
        {
            SqlCommand comSelect = new SqlCommand("SELECT CodInscrito, Nome, CertificadoLiberado FROM FaculdadeEncontroInscritos WHERE nome LIKE @nome AND CodEvento = @CodEvento AND efetivado = 1 ORDER BY Nome;");
            comSelect.Parameters.Add(new SqlParameter("CodEvento", Session["codEvento"].ToString()));
            comSelect.Parameters.Add(new SqlParameter("nome", "%" + this.txtbNomeInscrito.Text + "%"));
            DataTable dt = executarQuery(comSelect);

            if (dt != null && dt.Rows.Count > 0)
            {
                this.divAvisoSemGridView.Visible = false;
                gridViewInscritos.DataSource = dt;
                gridViewInscritos.DataBind();
            }
            else
            {
                gridViewInscritos.DataSource = null;
                gridViewInscritos.DataBind();
                this.divAvisoSemGridView.Visible = true;
            }
        }
        catch (Exception exc)
        {
            MetodoException(exc.Message, "Pesquisar_Click");
        }
    }

    private void MetodoException(string excMensagem, string metodo)
    {
        gridViewInscritos.DataSource = null;
        gridViewInscritos.DataBind();
        this.lblErro.Text = "Erro: " + excMensagem + "|-" + metodo + "-|";
        this.divErro.Visible = true;
        this.divPesquisaAluno.Visible = false;
    }

    private DataTable executarQuery(SqlCommand command)
    {
        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);

        try
        {
            command.Connection = conn;
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(dt);
        }
        catch { throw; }
        finally { conn.Close(); }

        return dt;
    }
}
